Imports DevExpress.XtraEditors
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Public Class DialogUser
    Inherits templateFormDialog
    Private contsparam() As String = {"username", "pass", "namalengkap", "koderole", "alamat", "nomorhp"}
    Public Overrides Sub SetReset()
        MyBase.SetReset()
        Me.Close()
    End Sub

    Public Sub RefreshComRole()
        Dim dt As New DataTable
        If Utils.executeSP("proc_role", New Object() {"select", Convert.DBNull, Convert.DBNull}, dt) Then
            If Utils.RowExist(dt) Then
                Dim bind As New Binding("dbinder", dt, "role")
                koderole.Properties.DataSource = dt
                koderole.Properties.DisplayMember = "role"
                koderole.Properties.ValueMember = "koderole"
                koderole.EditValue = dt(0)("koderole")
                'koderole.
                'ComRolePetugas.ValueMember = "koderole"
                ''ComRolePetugas.Text = ""
                'If ComRolePetugas.SelectedIndex = -1 Then ComRolePetugas.SelectedIndex = 0
            End If

        End If
    End Sub

    Public Sub RefreshTablePetugas()
        Dim dt As New DataTable
        Dim dsrole As New DataSet
        If Utils.executeSP("proc_petugas", New Object() {"select", username.Text, pass.Text, namalengkap.Text, koderole.GetColumnValue("koderole"), alamat.Text, nomorhp.Text}, dt) Then
            If Utils.RowExist(dt) Then
                GridControl1.DataSource = dt
            End If
        End If
    End Sub

    Private Sub DialogUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshComRole()
        RefreshTablePetugas()
    End Sub

    Public Overrides Sub SetSimpan()
        MyBase.SetSimpan()
        'MsgBox(koderole.GetColumnValue("koderole"))
        Dim retval As Boolean = True
        If username.Text = "" Then
            retval = False
        End If
        If pass.Text = "" Then
            retval = False
        End If
        If namalengkap.Text = "" Then
            retval = False
        End If
        If IsDBNull(koderole.GetColumnValue("koderole")) Then
            retval = False
        End If

        If Not retval Then
            MsgBox("Data Input Tidak Valid", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        Dim alamatset As String
        Dim bf As New BinaryFormatter
        Dim ms As New MemoryStream
        Dim bytearr As Byte()
        If alamat.Lines.Count > 0 Then
            ms = New MemoryStream
            bf.Serialize(ms, alamat.Lines)
            bytearr = ms.ToArray
            Dim vmemo As String = String.Empty
            For i As Integer = 0 To alamat.Lines.Count - 1
                vmemo += alamat.Lines(i).ToString
                If i < alamat.Lines.Count - 1 Then
                    vmemo += "\n"
                End If
            Next
            alamatset = vmemo

        Else

        End If
        Dim dt As New DataTable
        If Utils.executeSP("proc_petugas", New Object() {execvalue, username.Text, pass.Text, namalengkap.Text, koderole.GetColumnValue("koderole"), namalengkap.Text, nomorhp.Text}, dt) Then
            If dt IsNot Nothing Then
                MessageBox.Show(dt.Rows(0)("pesan").ToString)
                refreshEdit(False)
                RefreshTablePetugas()
            End If
        End If

    End Sub

    Private Sub GridView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.Click
        Dim obj As DataRow
        obj = GridView1.GetDataRow(GridView1.FocusedRowHandle)
        For Each sctl As String In contsparam
            Dim ctl As Control()
            ctl = Me.Controls.Find(sctl, True)
            If ctl IsNot Nothing And ctl.Length > 0 Then
                If Not ctl(0).Name = "pass" Then
                    If TypeOf ctl(0) Is MemoEdit Then
                        Dim ctltxt As New MemoEdit
                        ctltxt = CType(ctl(0), MemoEdit)
                        Dim richEdit As New RichTextBox
                        richEdit.Text = IIf(IsDBNull(obj(sctl)), "", obj(sctl))
                        ctltxt.Lines = richEdit.Lines
                    ElseIf TypeOf ctl(0) Is TextEdit Then
                        Dim ctltxt As New TextEdit
                        ctltxt = CType(ctl(0), TextEdit)
                        ctltxt.Text = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)
                    ElseIf TypeOf ctl(0) Is LookUpEdit Then
                        Dim ctltxt As New LookUpEdit
                        ctltxt = CType(ctl(0), LookUpEdit)
                        ctltxt.EditValue = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)

                    ElseIf TypeOf ctl(0) Is DateEdit Then
                        Dim ctltxt As New DateEdit
                        ctltxt = CType(ctl(0), DateEdit)
                        ctltxt.DateTime = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)
                    End If
                End If

            End If

        Next
    End Sub
    Private Sub refreshEdit(ByVal aktif As Boolean)
        For Each sctl As String In contsparam
            Dim ctl As Control()
            ctl = Me.Controls.Find(sctl, True)
            If ctl IsNot Nothing And ctl.Length > 0 Then
                If TypeOf ctl(0) Is MemoEdit Then
                    Dim ctltxt As New MemoEdit
                    ctltxt = CType(ctl(0), MemoEdit)
                    ctltxt.Text = ""
                    If Not aktif Then
                        ctltxt.Properties.ReadOnly = True
                    Else
                        ctltxt.Properties.ReadOnly = False
                    End If
                ElseIf TypeOf ctl(0) Is TextEdit Then
                    Dim ctltxt As New TextEdit
                    ctltxt = CType(ctl(0), TextEdit)
                    ctltxt.Text = ""
                    If Not aktif Then
                        ctltxt.Properties.ReadOnly = True
                    Else
                        ctltxt.Properties.ReadOnly = False
                    End If
                ElseIf TypeOf ctl(0) Is LookUpEdit Then
                    Dim ctltxt As New LookUpEdit
                    ctltxt = CType(ctl(0), LookUpEdit)
                    ctltxt.EditValue = 0
                    If Not aktif Then
                        ctltxt.Properties.ReadOnly = True
                    Else
                        ctltxt.Properties.ReadOnly = False
                    End If
                End If
            End If

        Next

    End Sub
    Private execvalue As String
    Private Sub RadioGroup1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioGroup1.SelectedIndexChanged
        If RadioGroup1.SelectedIndex = 0 Then
            refreshEdit(True)
            execvalue = "add"
        ElseIf RadioGroup1.SelectedIndex = 1 Then
            refreshEdit(True)
            GridView1_Click(sender, e)
            username.Properties.ReadOnly = True
            'role.Properties.ReadOnly = False
            execvalue = "edit"
        ElseIf RadioGroup1.SelectedIndex = 2 Then
            refreshEdit(False)
            pass.Properties.ReadOnly = False
            GridView1_Click(sender, e)
            execvalue = "delete"
        End If
    End Sub
End Class